package mashuaiproject.mashuaiproject.serivce;

import mashuaiproject.mashuaiproject.mapper.OtherMapper;
import mashuaiproject.mashuaiproject.mapper.QuesionMapper;
import mashuaiproject.mashuaiproject.mapper.UserMapper;
import mashuaiproject.mashuaiproject.model.Dongtai;
import mashuaiproject.mashuaiproject.model.Quesions;
import mashuaiproject.mashuaiproject.model.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class JMontiorSerivce {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private  OtherMapper otherMapper;
    @Autowired
    private QuesionMapper quesionMapper;
    //监控等级的变化
    public void Upleave(Integer userId){
        //全局监控等级是否发生变化
        Integer leave=1;//默认等级1
        Integer exp=userMapper.selectByExp(userId);
        Integer jifen=userMapper.selectByJifen(userId);
        Integer weiwnag=userMapper.selectByWeiwang(userId);
        if(exp>=100 && jifen>=1000 && weiwnag>=10){
            leave=2;
        }
        if((exp>100 && exp<=300)  && (jifen>1000 && jifen<=3000) && (weiwnag>10 && weiwnag<=30)){
            leave=3;
        }
        if((exp>300 && exp<=500)  && (jifen>3000 && jifen<=5000) && (weiwnag>30 && weiwnag<=50)){
            leave=4;
        }
        if((exp>500 && exp<=1000)  && (jifen>5000 && jifen<=10000) && (weiwnag>50 && weiwnag<=100)){
            leave=5;
        }
        if(exp>1000 && jifen>10000 && weiwnag>100){
            leave=6;
        }
        //更新等级
        Integer leaves=userMapper.updateLeave(userId,leave);
    }

    public Integer checkHasFristFatie(Integer id,String staues) {
        if (staues.equals("fatie")){
            //增加20经验，150积分，2威望
            Integer exp= otherMapper.updateExp(id,20);
            otherMapper.updateJifen(id,150);
            otherMapper.updateWeiwang(id,2);
            //判断用户是否重复获取这个勋章
            Integer isRepriet=otherMapper.selectIsHasSlefXZ(id,2);
            //新增的首次发帖子判断
            if(isRepriet==0){
                Integer insertXZ=otherMapper.insertXZ(id,2);
            }
        }else if(staues.equals("gengxin")){
            //增加20经验，150积分，2威望
            otherMapper.updateJifen(id,50);
            System.err.println("更新");
        }else if(staues.equals("comment")){
            Integer exp= otherMapper.updateExp(id,10);
            otherMapper.updateJifen(id,100);
            otherMapper.updateWeiwang(id,1);
        }
        return  1;
    }

    public void checkHasGuaZhu(Integer id, Integer boZhuId, String add) {
        if(add.equals("add")){
            System.err.println("关注");
            //增加关注：被关注的人：50经验,150积分，2威望，
            //	关注的人:50积分
            otherMapper.updateExp(boZhuId,50);
            otherMapper.updateJifen(boZhuId,150);
            otherMapper.updateWeiwang(boZhuId,2);
            otherMapper.updateJifen(id,50);

        }else if(add.equals("reds")){
            //取消关注
            otherMapper.updateExp(boZhuId,-50);
            otherMapper.updateJifen(boZhuId,-150);
            otherMapper.updateWeiwang(boZhuId,-2);
            otherMapper.updateJifen(id,-50);
        }else if(add.equals("saveAdd")){

            otherMapper.updateExp(boZhuId,50);
            otherMapper.updateJifen(boZhuId,50);
            otherMapper.updateWeiwang(boZhuId,1);
            otherMapper.updateJifen(id,20);
            System.err.println("添加收藏关注");

        }else if(add.equals("saveReds")){
            //根据文章id找出用户id
            Integer boZhu=otherMapper.selectBoZhuId(boZhuId);
            otherMapper.updateExp(boZhu,-50);
            otherMapper.updateJifen(boZhu,-50);
            otherMapper.updateWeiwang(boZhu,-1);
            otherMapper.updateJifen(id,-20);
            System.err.println("删除收藏");
        }
    }

    //插入动态
    // 1.发布文章
    //2.更新文章
    //3.关注博主
    //4.取消关注
    //5.收藏文章
    //6.取消收藏
    //7.领取勋章
    //8.排名
    //9.加好友
    public Integer insertDongtai(Integer id, int type, Object object) {
        //1.每个用户智能查看最近20条动态，如果超过20条自动删除多余的
        String  context=null;
        if(type==1){
            context="你发布了一篇辩题为："+object+"的文章，获得了20经验。150积分，2威望";
        }else if(type==2){
            context="你更新了辩题为："+object+"的文章，获得50积分";
        }else if(type==3){
            //根据博扼住id找出博主的user
            Integer bozhuId= (Integer) object;
            Users users=userMapper.findByUser(bozhuId);
            context="你关注了博主："+users.getName()+"，获得50积分·";
        }else if(type==4){
            Integer bozhuId= (Integer) object;
            Users users=userMapper.findByUser(bozhuId);
            context="你取消关注了博主："+users.getName()+"，失去50积分·";
        }else if(type==5){
            Integer queisonId= (Integer) object;
            Quesions quesions=quesionMapper.selectQueiosns(queisonId);
            Users users=userMapper.findByUser(quesions.getCreator());
            context="你收藏博主："+users.getName()+"，"+quesions.getTitle()+"的文章，获取了20积分·";
        }else if(type==6){
            Integer queisonId= (Integer) object;
            Quesions quesions=quesionMapper.selectQueiosns(queisonId);
            Users users=userMapper.findByUser(quesions.getCreator());
            context="你取消收藏博主："+users.getName()+"，"+quesions.getTitle()+"的文章，失去了20积分·";
        }else if(type==7){

            context=object+"，奖励200积分·";
        }else if(type==8){
            Users users=userMapper.findByUser(id);
            context="你今日的总排名排名为："+object+"，继续加油·";
        }else if(type==9){
            Integer friedId= (Integer) object;
            Users users=userMapper.findByUser(friedId);
            context="你申请加："+users.getName()+"为好友";
        }else if(type==10){
            Integer friedId= (Integer) object;
            Users users=userMapper.findByUser(friedId);
            context="用户："+users.getName()+"申请加你为好友";
        }else if(type==11){
            Integer useId= (Integer) object;
            Users users=userMapper.findByUser(useId);
            context="用户："+users.getName()+"同意你的好友申请";
        }else if(type==12){
            Integer useId= (Integer) object;
            Users users=userMapper.findByUser(useId);
            context="用户："+users.getName()+"拒绝你的好友申请";
        }
        //2.插入当的动态
        Dongtai dongtai=new Dongtai();
        dongtai.setUserId(id);
        dongtai.setType(type);
        dongtai.setContent(context);
        dongtai.setGmt_create(System.currentTimeMillis());
        Integer insertDongtai=otherMapper.insertDongTai(dongtai);
        return 1;
    }
}
